package tk.djcrazy.instantmessage.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.service.IChatMessageService;
import tk.djcrazy.instantmessage.service.IUserAvatarService;

import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport implements SessionAware {
	private static final long serialVersionUID = 6255990794690244929L;
	private File file;
	private String fileFileName;
	private Map<String, Object> session;
	private int userId;
	@Resource
	IUserAvatarService userAvatarService;
	@Resource
	IChatMessageService chatMessageService;

	public String execute() {
		System.out.println("file name:"+fileFileName);
		try {
			String path = "avatarfile/"+userAvatarService.storeAvatar(file, fileFileName);
			chatMessageService.addMessage((Integer) session.get("uid"), userId,
					"对方向您传送文件，请点击下载：{a style=\"color: blue\" href=\"" + path + "\"}点击下载文件{/a}");
		} catch (IOException e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public IUserAvatarService getUserAvatarService() {
		return userAvatarService;
	}

	public void setUserAvatarService(IUserAvatarService userAvatarService) {
		this.userAvatarService = userAvatarService;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public IChatMessageService getChatMessageService() {
		return chatMessageService;
	}

	public void setChatMessageService(IChatMessageService chatMessageService) {
		this.chatMessageService = chatMessageService;
	}

}
